package com.dataaccess.dataaccess.web.service;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseService;
import com.dataaccess.dataaccess.model.GasAlarmDeviceDataChanged;
import com.dataaccess.dataaccess.support.model.RespEntity;
import com.dataaccess.dataaccess.support.utils.HexUtils;
import com.dataaccess.dataaccess.web.mapper.GasAlarmDeviceDataChangedMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class DeviceAboatGasService extends BaseService {
    @Resource
    GasAlarmDeviceDataChangedMapper gasAlarmDeviceDataChangedMapper;
    @Resource
    private AsyncService asyncService;

    /**
     * @Author YangYu
     * @Description 赛特威尔可燃气体探测器
     * @Date Created in 14:05 2021/7/6
     * @Param [jsonParam]
     */

    public RespEntity siTerWell(JSONObject jsonParam) throws Exception {

        //筛选无用的数据
        if (!jsonParam.containsKey("payload")) {
            return RespEntity.success();
        }
        if (!jsonParam.getJSONObject("payload").containsKey("gas_value")) {
            return RespEntity.success();
        }
        //数据
        JSONObject payload = jsonParam.getJSONObject("payload");
        //设备号
        String number = jsonParam.getStr("IMEI");
        //检测介质 0：天然气； 1：液化石油气； 2：煤层气； 3：一氧化碳； 4：硫化氢
        String medium = payload.getStr("medium");
        //气体浓度百分比
        String gasValue = payload.getStr("gas_value");
        //报警时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("number", number);
        //报警
        if (!payload.getStr("gas_sensor_state").equals("0")) {
            jsonObject.put("alarmtime", time);
            jsonObject.put("deviceClassify", "4");
            jsonObject.put("interfaceType", "1");
            jsonObject.put("alarmTypeName", "可燃气设备CH4报警");
            jsonObject.put("content", "可燃气设备CH4报警" + " 浓度：" + gasValue + "%LEL");
            //报警原始数据存储
            GasAlarmDeviceDataChanged gasAlarmDeviceDataChanged = new GasAlarmDeviceDataChanged();
            gasAlarmDeviceDataChanged.setId(UUID.randomUUID().toString());
            gasAlarmDeviceDataChanged.setJsondata(jsonParam.toString());
            gasAlarmDeviceDataChanged.setType("siTerWell");
            gasAlarmDeviceDataChangedMapper.insert(gasAlarmDeviceDataChanged);
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
        } else {
            //异步更新设备状态
            jsonObject.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonObject);
        }
        //正常数据
        JSONArray jSONArray = new JSONArray();
        JSONObject object = new JSONObject();
        object.put("name", "甲烷");
        object.put("value", gasValue);
        jSONArray.add(object);
        jsonObject.put("paraminfo", jSONArray);
        jsonObject.put("time", time);
        fireFightingiot(jsonObject, "firefightingiot_data", "normalInfo", "device");
        return RespEntity.success();
    }

    //派安一氧化碳
    public RespEntity panAnCO(JSONObject jsonParam) throws Exception {
        String s = jsonParam.getJSONObject("payload").getJSONObject("serviceData").getStr("content");
        //IMEI号
        String number = jsonParam.getStr("IMEI");
        //报警时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        //命令
        String command = s.substring(0, 2);
        int value = 0;
        //正常数据
        JSONObject jsonParamN = new JSONObject();
        jsonParamN.put("number", number);
        //报警、故障
        if (command.equals("03")) {
            //警告类型
            String type = s.substring(4, 6);
            //警告内容Code
            String contentCode = s.substring(8, 10);
            //数值高位
            String valueMax = s.substring(10, 12);
            //数值地位
            String valueMin = s.substring(12, 14);
            //数值类型
            String valueTypeCode = s.substring(14, 16);
            String alarmTypeName = "";
            String valueType = "";
            //报警内容
            value = Integer.parseInt(valueMax + valueMin, 16);
            if (valueTypeCode.toLowerCase().equals("04")) {
                valueType = "PPM";
            }
            String content = "";
            //将大写全部转化为小写
            String contentCodeLower = contentCode.toLowerCase();
            if (contentCodeLower.equals("0c")) {
                alarmTypeName = "一氧化碳超标";
                content = alarmTypeName + value + valueType;
            } else if (contentCodeLower.equals("4f")) {
                alarmTypeName = "一氧化碳超标";
                content = alarmTypeName + value + valueType;
            }

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("deviceClassify", "4");
            jsonObject.put("number", number);
            jsonObject.put("interfaceType", "1");
            jsonObject.put("alarmtime", time);
            jsonObject.put("alarmTypeName", alarmTypeName);
            jsonObject.put("content", content);
            //报警原始数据存储
            GasAlarmDeviceDataChanged gasAlarmDeviceDataChanged = new GasAlarmDeviceDataChanged();
            gasAlarmDeviceDataChanged.setId(UUID.randomUUID().toString());
            gasAlarmDeviceDataChanged.setJsondata(jsonParam.toString());
            gasAlarmDeviceDataChanged.setType("CO");
            gasAlarmDeviceDataChangedMapper.insert(gasAlarmDeviceDataChanged);
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
        } else {
            //异步更新设备状态
            jsonParamN.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonParamN);
        }

        JSONObject jSONObject = new JSONObject();
        JSONArray jSONArray = new JSONArray();
        jSONObject.put("name", "一氧化碳");
        jSONObject.put("value", value);
        jSONArray.add(jSONObject);
        jsonParamN.put("paraminfo", jSONArray);
        jsonParamN.put("time", time);
        fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");

        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 思维智安可燃气体探测器
     * @Date Created in 14:05 2021/7/6
     * @Param [jsonParam]
     */

    public RespEntity thinKingZhiAnGas(JSONObject jsonParam) throws Exception {

        //数据
        JSONObject payload = jsonParam.getJSONObject("payload");
        //设备号
        String number = jsonParam.getStr("IMEI");
        //检测介质 0：天然气； 1：液化石油气； 2：煤层气； 3：一氧化碳； 4：硫化氢
        String medium = payload.getStr("medium");
        //气体浓度百分比
        String gasValue = payload.getStr("gas_value");
        //报警时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("number", number);
        //报警
        if (!payload.getStr("gas_sensor_state").equals("0")) {
            jsonObject.put("alarmtime", time);
            jsonObject.put("deviceClassify", "4");
            jsonObject.put("interfaceType", "1");
            jsonObject.put("alarmTypeName", "可燃气设备CH4报警");
            jsonObject.put("content", "可燃气设备CH4报警" + " 浓度：" + gasValue + "%LEL");
            //报警原始数据存储
            GasAlarmDeviceDataChanged gasAlarmDeviceDataChanged = new GasAlarmDeviceDataChanged();
            gasAlarmDeviceDataChanged.setId(UUID.randomUUID().toString());
            gasAlarmDeviceDataChanged.setJsondata(jsonParam.toString());
            gasAlarmDeviceDataChanged.setType("siTerWell");
            gasAlarmDeviceDataChangedMapper.insert(gasAlarmDeviceDataChanged);
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
        } else {
            //异步更新设备状态
            jsonObject.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonObject);
        }
        //正常数据
        JSONArray jSONArray = new JSONArray();
        JSONObject object = new JSONObject();
        object.put("name", "甲烷");
        object.put("value", gasValue);
        jSONArray.add(object);
        jsonObject.put("paraminfo", jSONArray);
        jsonObject.put("time", time);
        fireFightingiot(jsonObject, "firefightingiot_data", "normalInfo", "device");
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 派安可燃气体（NB8201）
     * @Date Created in 9:58 2022/8/24
     * @Param [jsonParam]
     */

    public RespEntity pa8201Gas(JSONObject jsonParam) throws Exception {
        //筛选无用的数据
        if (!jsonParam.containsKey("payload")) {
            return RespEntity.success();
        }
        //数据
        String code = HexUtil.encodeHexStr(Base64Decoder.decode(jsonParam.getJSONObject("payload").getStr("APPdata")));
        //设备号
        String number = jsonParam.getStr("IMEI");
        //时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        //报警类型
        String alarmTypeName = "";
        //报警内容
        String content = "";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("number", number);
        //报警值
        Integer value = 0;
        if (code.length() == 28) {
            //警告状态
            String state = code.substring(14, 16);
            //报警值
            value = Integer.parseInt(code.substring(20, 24), 16);
            if (state.equals("01")) {
                alarmTypeName = "可燃气设备报警";
                content = "可燃气设备报警" + " 浓度：" + value + "%LEL";
            } else {
                alarmTypeName = "可燃气体设备故障";
                content = "可燃气体设备故障";
            }
            jsonObject.put("alarmtime", time);
            jsonObject.put("deviceClassify", "4");
            jsonObject.put("interfaceType", "1");
            jsonObject.put("alarmTypeName", alarmTypeName);
            jsonObject.put("content", content);
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
        } else {
            //异步更新设备状态
            jsonObject.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonObject);
        }
        //正常数据
        JSONArray jSONArray = new JSONArray();
        JSONObject object = new JSONObject();
        object.put("name", "甲烷");
        object.put("value", value);
        jSONArray.add(object);
        jsonObject.put("paraminfo", jSONArray);
        jsonObject.put("time", time);
        fireFightingiot(jsonObject, "firefightingiot_data", "normalInfo", "device");
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 派安可燃气体（NB8020对接第三方平台）
     * @Date Created in 9:58 2022/8/24
     * @Param [jsonParam]
     */

    public RespEntity pa8020Gas(JSONObject jsonParam) throws Exception {
        //数据
        JSONObject devInfo = jsonParam.getJSONObject("devInfo");
        JSONObject iotMsg = jsonParam.getJSONObject("iotMsg");
        //设备编号
        String number = iotMsg.getStr("devId");
        // 预警类型
        int msgType = iotMsg.getInt("msgType");
        // 报警描述
        String msgDesc = iotMsg.getStr("msgDesc");
        //告警类别
        String alarmType = "";
        //报警内容
        String content = "";
        //报警标识
        boolean alarmFlag = false;
        //报警时间
        String time = contextLoads(iotMsg.getLong("createTime"));
        JSONObject jsonParamN = new JSONObject();
        jsonParamN.put("number", number);
        if (msgType == 1) {
            //异步更新设备状态
            jsonParamN.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonParamN);
            return RespEntity.success();
        }
        if (msgType == 2) {
            alarmType = "可燃气设备报警";
            content = msgDesc;
            alarmFlag = true;
        } else if (msgType == 3) {
            alarmType = "可燃气体设备故障";
            content = msgDesc;
            alarmFlag = true;
        }
        if (alarmFlag) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("deviceClassify", "3");
            jsonObject.put("number", number);
            jsonObject.put("interfaceType", "1");
            jsonObject.put("alarmtime", time);
            jsonObject.put("alarmTypeName", alarmType);
            jsonObject.put("content", content);
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            jsonParamN.put("state", "2");
            //异步更新传输装置状态
            asyncService.updateDeviceCurrentState(jsonParamN);
        } else {
            jsonParamN.put("time", DateUtil.now());
            //异步更新部件状态
            asyncService.update_user_transmission_state(jsonParamN);
        }
        //正常数据
        JSONArray jSONArray = new JSONArray();
        JSONObject object = new JSONObject();
        object.put("name", "甲烷");
        object.put("value", 0);
        jSONArray.add(object);
        jsonParamN.put("paraminfo", jSONArray);
        jsonParamN.put("time", time);
        fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 三江可燃气体
     * @Date Created in 8:51 2022/10/27
     * @Param [jsonParam]
     */

    public RespEntity sanjiangGas(JSONObject jsonParam) {
        //设备号
        String number = jsonParam.getJSONObject("ext").getStr("IMEI");
        //时间
        String time = jsonParam.getStr("at");
        //报警数据
        String data = jsonParam.getStr("data");
        //报警类型名称
        String alarmTypeName = "";
        //报警内容
        String content = "";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("number", number);
        //报警标识
        boolean flag = false;
        if (data.equals("危险浓度可燃气体警报")) {
            alarmTypeName = "可燃气设备报警";
            content = "危险浓度可燃气体警报";
            flag = true;
        } else if (data.equals("设备自检")) {
            alarmTypeName = "可燃气正常";
            content = "设备自检";
            flag = true;
        }
        if (flag) {
            jsonObject.put("alarmtime", time);
            jsonObject.put("deviceClassify", "4");
            jsonObject.put("interfaceType", "1");
            jsonObject.put("alarmTypeName", alarmTypeName);
            jsonObject.put("content", content);
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
        }
        if (data.equals("设备心跳")) {
            //大数据调用
            jsonObject.put("dataType", "2");
            fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            //正常数据
            JSONArray jSONArray = new JSONArray();
            JSONObject object = new JSONObject();
            object.put("name", "甲烷");
            object.put("value", 0);
            jSONArray.add(object);
            jsonObject.put("paraminfo", jSONArray);
            jsonObject.put("time", time);
            fireFightingiot(jsonObject, "firefightingiot_data", "normalInfo", "device");
            //异步更新设备状态
            jsonObject.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonObject);
        }
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 一氧化碳（吉凯达）
     * @Date Created in 8:51 2023/4/12
     * @Param [jsonParam]
     */

    public RespEntity jikaidaCarbonMonoxide(JSONObject jsonParam) throws Exception {
        //数据类型
        String messageType = jsonParam.getStr("messageType");
        if (!messageType.equals("dataReport") && !messageType.equals("eventReport")) {
            return RespEntity.success();
        }
        //设备号
        String number = jsonParam.getStr("IMEI");
        //报警时间
        String time = contextLoads(jsonParam.getLong("timestamp"));
        //温度
        Integer temperature = 0;
        //电池电量
        String battery = "0";
        //CSQ信号强度
        Integer csq = 0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("number", number);
        if (messageType.equals("dataReport")) {
            JSONObject payload = jsonParam.getJSONObject("payload");
            battery = String.format("%.2f", payload.getInt("voltage") / 1000.0);
            temperature = payload.getInt("temperature");
            csq = payload.getInt("rssi");
        } else if (messageType.equals("eventReport")) {
            JSONObject eventContent = jsonParam.getJSONObject("eventContent");
            battery = String.format("%.2f", eventContent.getInt("voltage") / 1000.0);
            temperature = eventContent.getInt("temperature");
            csq = eventContent.getInt("rssi");
            String payload = eventContent.getStr("payload");
            //报警类型名称
            String alarmTypeName = "";
            //报警内容
            String content = "";
            //报警标识
            boolean flag = false;
            if ("55AA01015A".equals(payload)) {
                alarmTypeName = "一氧化碳报警";
                content = alarmTypeName;
                flag = true;
            } else if ("55AA01025A".equals(payload)) {
                alarmTypeName = "一氧化碳设备低电量";
                content = alarmTypeName + " 电量:" + battery + "V";
                flag = true;
            } else if ("55AA01045A".equals(payload)) {
                alarmTypeName = "一氧化碳设备拆除";
                content = alarmTypeName;
                flag = true;
            } else if ("55AA01055A".equals(payload)) {
                alarmTypeName = "一氧化碳设备自检";
                content = alarmTypeName;
                flag = true;
            } else if ("55AA01075A".equals(payload)) {
                alarmTypeName = "一氧化碳设备故障";
                content = alarmTypeName;
                flag = true;
            } else if ("55AA01085A".equals(payload)) {
                alarmTypeName = "温度报警(一氧化碳)";
                content = alarmTypeName + " 温度值:" + temperature + "℃";
                flag = true;
            } else if ("55AA01095A".equals(payload)) {
                alarmTypeName = "温度报警(一氧化碳)";
                content = alarmTypeName;
                flag = true;
            }
            if (flag) {
                jsonObject.put("alarmtime", time);
                jsonObject.put("deviceClassify", "4");
                jsonObject.put("interfaceType", "1");
                jsonObject.put("alarmTypeName", alarmTypeName);
                jsonObject.put("content", content);
                fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
                //异步更新设备状态
                jsonObject.put("state", "2");
                asyncService.updateDeviceCurrentState(jsonObject);
            }
        } else {
            //异步更新设备状态
            jsonObject.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonObject);
        }
        JSONObject jsonParamN = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(getNormalItem("温度", temperature));
        jsonArray.add(getNormalItem("电量", battery));
        jsonArray.add(getNormalItem("信号", csq));
        //正常数据
        jsonParamN.put("number", number);
        jsonParamN.put("time", time);
        jsonParamN.put("paraminfo", jsonArray);
        jsonParamN.put("dataType", "2");
        fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
        fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 汉威燃气（JY_KBR2_祥云）
     * @Date Created in 9:44 2023/4/25
     * @Param [jsonParam]
     */

    public RespEntity hanWei_JY_KBR2(JSONObject jsonParam) {
        logger.info("hanWei_JY_KBR2_service" + "------" + jsonParam);
        //设备号
        String number = jsonParam.getStr("deviceCode");
        //时间
        String time = jsonParam.getStr("eventTime");
        //报警类型名称
        String alarmTypeName = "";
        //报警内容
        String content = "";
        JSONObject jsonParamN = new JSONObject();
        //正常数据
        jsonParamN.put("number", number);
        //报警标识
        boolean flag = false;
        try {
            JSONObject payload = jsonParam.getJSONObject("payload");
            JSONObject json = (JSONObject) payload.getJSONArray("sensorMsg").get(0);
            String sensorType = json.getStr("sensorType").replace(" ", "");
            //计算浓度数据
            BigDecimal bigdecimal = new BigDecimal(json.getStr("sensorVal"));
            //浓度值
            BigDecimal sensorVal = bigdecimal.movePointLeft(json.getInt("deciPoint"));
            if (payload.getStr("deviceStatus").equals("低报")) {
                alarmTypeName = "可燃气设备报警";
                content = "可燃气设备" + sensorType + "报警" + sensorVal + json.getStr("sensorValUnit");
                flag = true;
            } else if (payload.getStr("deviceStatus").equals("掉电")) {
                alarmTypeName = "可燃气体设备低电量";
                content = "可燃气体设备低电量、掉电报警";
                flag = true;
            } else if (payload.getStr("deviceStatus").equals("传感器错误")) {
                alarmTypeName = "可燃气体设备故障";
                content = "可燃气体设备故障、传感器错误";
                flag = true;
            }
            if (flag) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("number", number);
                jsonObject.put("alarmtime", time);
                jsonObject.put("deviceClassify", "4");
                jsonObject.put("interfaceType", "1");
                jsonObject.put("alarmTypeName", alarmTypeName);
                jsonObject.put("content", content);
                fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
                //异步更新设备状态
                jsonObject.put("state", "2");
                asyncService.updateDeviceCurrentState(jsonObject);
            } else {
                //异步更新设备状态
                jsonParamN.put("state", "1");
                asyncService.updateDeviceCurrentState(jsonParamN);
            }
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(getNormalItem("甲烷", sensorVal));
            jsonParamN.put("time", time);
            jsonParamN.put("paraminfo", jsonArray);
            jsonParamN.put("dataType", "2");
            fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
            fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
            logger.info("正常数据结束");
        } catch (Exception e) {
            System.out.println("hanWei_JY_KBR2_Exception:---" + e);
        }

        return RespEntity.success();
    }

    public JSONObject getNormalItem(String name, Object value) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
        jsonObject.put("value", value);
        return jsonObject;
    }

    /**
     * 将毫秒转化为日期(格式:yyyy-MM-dd hh:mm:ss)
     *
     * @param l
     * @return
     * @throws Exception
     */
    public String contextLoads(Long l) throws Exception {

        java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        Date date = new Date(l);
        String str1 = sdf1.format(date);
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date2 = sdf.parse(str1);
        String str2 = sdf.format(date2);
        return str2;
    }


    /**
     * @Author YangYu
     * @Description 工业燃气报警（京电云）
     * @Date Created in 15:35 2024/6/26
     * @Param [jsonParam]
     */

    public RespEntity jdy_gas(JSONObject jsonParam) {
        //实时数据707013A1000101020304050102011200000209000401
        String data = jsonParam.getStr("code");
        //命令字
        String commandWord = data.substring(6, 8);
        //网关ID
        String gatewayId = data.substring(12, 22);
        //主机地址
        String hostAddress = data.substring(22, 24);
        //时间
        String time = DateUtil.now();
        //报警类型名称
        String alarmTypeName = "";
        //报警内容
        String content = "";
        if (commandWord.equals("a1")) {
            JSONObject jsonParamN = new JSONObject();
            //探测器个数
            Integer count = HexUtils.hexToInt(data.substring(24, 26));
            //数据长度
            int dataCount = count * 10;
            //探测器数据
            String detectorData = data.substring(26, 26 + dataCount);
            for (int i = 0; i < detectorData.length(); i += 10) {
                if (i + 10 <= detectorData.length()) {
                    String sub = detectorData.substring(i, i + 10);
                    System.out.println(sub);
                    //探测器地址
                    String detectorAddr = sub.substring(0, 2);
                    //燃气浓度
                    Integer gasConcentration = HexUtils.hexToInt(sub.substring(2, 6));
                    //正常数据
                    jsonParamN.put("number", gatewayId + hostAddress + detectorAddr);
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(getNormalItem("甲烷", gasConcentration));
                    jsonParamN.put("time", time);
                    jsonParamN.put("paraminfo", jsonArray);
                    jsonParamN.put("dataType", "2");
                    fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
                    //设备状态报警、故障状态 11111111
                    String state = HexUtils.hexToBinaryString(sub.substring(6, 10));
                    if (state.charAt(7) == '0' && state.charAt(6) == '0' && state.charAt(5) == '0' && state.charAt(4) == '0') {
                        JSONObject jsonParams = new JSONObject();
                        jsonParams.put("number", gatewayId + hostAddress + detectorAddr);
                        //异步更新设备状态
                        jsonParams.put("state", "1");
                        asyncService.updateDeviceCurrentState(jsonParams);
                    }
                }
            }
        } else if (commandWord.equals("a3")) {
            //探测器地址
            String detectorAddr = data.substring(24, 26);
            //设备状态报警、故障状态 11111111
            String state = HexUtils.hexToBinaryString(data.substring(26, 30));
            //报警数据
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("number", gatewayId + hostAddress + detectorAddr);
            jsonObject.put("alarmtime", time);
            jsonObject.put("deviceClassify", "4");
            jsonObject.put("interfaceType", "1");
            if (state.charAt(7) == '1') {
                alarmTypeName = "可燃气体设备故障";
                content = "通讯故障";
                jsonObject.put("alarmTypeName", alarmTypeName);
                jsonObject.put("content", content);
                fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            }
            if (state.charAt(6) == '1') {
                alarmTypeName = "可燃气体设备故障";
                content = "探测器故障";
                jsonObject.put("alarmTypeName", alarmTypeName);
                jsonObject.put("content", content);
                fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            }
            if (state.charAt(5) == '1') {
                alarmTypeName = "可燃气设备报警";
                content = "高限报警";
                jsonObject.put("alarmTypeName", alarmTypeName);
                jsonObject.put("content", content);
                fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            }
            if (state.charAt(4) == '1') {
                alarmTypeName = "可燃气设备报警";
                content = "低限报警";
                jsonObject.put("alarmTypeName", alarmTypeName);
                jsonObject.put("content", content);
                fireFightingiot(jsonObject, "communication", "notificationAlarmHandel", "alarmInfo");
            }
            //异步更新设备状态
            jsonObject.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject);
            //异步更新网管设备状态
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("number", gatewayId);
            jsonObject1.put("state", "2");
            asyncService.updateDeviceCurrentState(jsonObject1);
        }else if (commandWord.equals("a2")) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("number", gatewayId);
            jsonObject.put("deviceId",jsonParam.getStr("dev_id"));
            asyncService.updateDeviceDownDevId(jsonObject);
            //异步更新设备状态
            jsonObject.put("state", "1");
            asyncService.updateDeviceCurrentState(jsonObject);
        }
        return RespEntity.success();
    }

}
